package it.gestionedocumentale.spring.service;

import it.gestionedocumentale.hibernate.dao.ProfiloDAO;
import it.gestionedocumentale.hibernate.sessionfactory.HibernateSessionFactory;
import it.gestionedocumentale.model.Profilo;

import java.util.List;

import org.hibernate.Transaction;

public class ProfiloServiceImpl implements ProfiloService {

	private ProfiloDAO profiloDAO;

	public void setProfiloDAO(ProfiloDAO profiloDAO) {
		this.profiloDAO = profiloDAO;
	}

	public ProfiloDAO getProfiloDAO() {
		return profiloDAO;
	}

	@Override
	public void save(Profilo transientInstance) throws Exception {
		Transaction tx = HibernateSessionFactory.getSession()
				.beginTransaction();
		profiloDAO.save(transientInstance);
		tx.commit();

	}

	@Override
	public void delete(Profilo persistentInstance) throws Exception {
		Transaction tx = HibernateSessionFactory.getSession()
				.beginTransaction();
		profiloDAO.delete(persistentInstance);
		tx.commit();
	}
	
	@Override
	public Profilo findById(Long idProfilo) throws Exception {
		Transaction tx = HibernateSessionFactory.getSession()
				.beginTransaction();
		Profilo profilo = profiloDAO.findById(idProfilo);
		tx.commit();
		return profilo;
	}

	@Override
	public List<Profilo> findByExample(Profilo instance) throws Exception {
		Transaction tx = HibernateSessionFactory.getSession()
				.beginTransaction();
		List<Profilo> gruppi = profiloDAO.findByExample(instance);
		tx.commit();
		return gruppi;
	}

	@Override
	public List<Profilo> findAll() throws Exception {
		Transaction tx = HibernateSessionFactory.getSession()
				.beginTransaction();
		List<Profilo> profili = profiloDAO.findAll();
		tx.commit();
		return profili.size() < 1 ? null : profili;
	}

}
